package com.pxmeta.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pxmeta.common.constant.HttpStatus;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.dynamic.entity.Topic;
import com.pxmeta.dynamic.entity.TopicCategory;
import com.pxmeta.dynamic.entity.dto.CreateTopicCategoryDTO;
import com.pxmeta.dynamic.entity.dto.CreateTopicDTO;
import com.pxmeta.dynamic.entity.dto.TopicDTO;
import com.pxmeta.dynamic.entity.vo.TopicCategoryVO;
import com.pxmeta.dynamic.service.ITopicCategoryService;
import com.pxmeta.dynamic.service.ITopicService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "话题管理", tags = "话题")
@CrossOrigin
@RequestMapping(value = "/topic")
@RequiredArgsConstructor
public class TopicController {

    private final ITopicService topicService;
    private final ITopicCategoryService topicCategoryService;


    @GetMapping("/getList")
    public AjaxResult<List<TopicDTO>> getList(@RequestParam Integer page,
                                              @RequestParam Integer size,
                                              @RequestParam(required = false) String keyword,
                                              @RequestParam Integer type,
                                              @RequestParam(required = false) Long categoryId) {
        IPage<TopicDTO> result = topicService.getList(page, size, keyword, type, categoryId);
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }


    @GetMapping("/getCategoryList")
    public AjaxResult<List<TopicCategoryVO>> getCategoryList(@RequestParam Integer page,
                                                             @RequestParam Integer size,
                                                             @RequestParam(required = false) String keyword,
                                                             @RequestParam Integer type) {
//        IPage<TopicDTO> result = topicService.getList(page,size,keyword, type);
        IPage<TopicCategoryVO> result = topicCategoryService.getList(page, size, keyword, type);
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }


    @PostMapping(value = "/createCategory")
    public AjaxResult createCategory(@RequestBody CreateTopicCategoryDTO dto){
        TopicCategory category = new TopicCategory();
        BeanUtils.copyProperties(dto,category);
        topicCategoryService.save(category);
        return AjaxResult.success();
    }

    @PostMapping(value = "/createTopic")
    public AjaxResult createTopic(@RequestBody CreateTopicDTO dto){
        Long categoryId = dto.getCategoryId();
        TopicCategory category = topicCategoryService.getById(categoryId);
        if (null == category){
            return AjaxResult.error(HttpStatus.DATA_NOT_FOUND_ERROR,"分类不存在！");
        }
        Topic topic = new Topic();
        topic.setCircleType(category.getCircleType());
        topic.setName(dto.getName());
        topic.setCircleId(dto.getCircleId());
        topic.setImgUrl(dto.getImgUrl());
        topic.setCategoryId(categoryId);
        topicService.save(topic);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteById/{id}")
    public AjaxResult<Void> deleteById(@PathVariable Long id) {
        topicService.deleteTopic(id);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteCategoryById/{id}")
    public AjaxResult<Void> deleteCategoryById(@PathVariable Long id) {
        topicCategoryService.removeById(id);
        return AjaxResult.success();
    }

    @PutMapping("/update")
    public AjaxResult<Void> deleteById(@RequestBody TopicDTO dto) {
        Topic topic = new Topic();
        topic.setId(dto.getId());
        if (StringUtils.isNotBlank(dto.getName())) {
            topic.setName(dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getImgUrl())) {
            topic.setImgUrl(dto.getImgUrl());
        }
        topicService.updateById(topic);
        return AjaxResult.success();
    }
}
